package com.red.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.red.context.BaseContext;
import com.red.dto.ShoppingCarDTO;
import com.red.entity.Products;
import com.red.entity.ShoppingCar;
import com.red.mapper.ProductsMapper;
import com.red.mapper.ShoppingCarMapper;
import com.red.result.PageResult;
import com.red.service.ShoppingCarService;
import com.red.vo.ShoppingCarVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class ShoppingCarServiceImpl extends ServiceImpl<ShoppingCarMapper, ShoppingCar> implements ShoppingCarService {

    @Autowired
    private ShoppingCarMapper shoppingCarMapper;
    @Autowired
    private ProductsMapper productsMapper;

    /**
     * 分页查询

     * @return
     */
    public List<ShoppingCarVO> pageQuery() {
        List<ShoppingCar> shoppingCars = shoppingCarMapper.selectList(new QueryWrapper<>());

        ArrayList<ShoppingCarVO> arrayList = new ArrayList<>();
        for (ShoppingCar shoppingCar : shoppingCars) {
            ShoppingCarVO shoppingCarVO = new ShoppingCarVO();
            Products products = productsMapper.selectById(shoppingCar.getProductId());
            BeanUtils.copyProperties(shoppingCar,shoppingCarVO);
            shoppingCarVO.setProducts(products);
            arrayList.add(shoppingCarVO);
        }
        return arrayList;
    }

    /**
     * 添加购物车
     * @param dto
     */
    public void insert(ShoppingCarDTO dto) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("product_id",dto.getProductId());
//        map.put("user_id",BaseContext.getCurrentId());
        map.put("user_id",1);
        //查询购物车是否有数据
        List<ShoppingCar> shoppingCars = shoppingCarMapper.selectByMap(map);
        //有数据对数量+1
        if (shoppingCars!= null && shoppingCars.size()>0){
            ShoppingCar shoppingCar = shoppingCars.get(0);
            shoppingCar.setNumber(shoppingCar.getNumber()+1);
            shoppingCarMapper.updateById(shoppingCar);
        }else{
            //没有数据添加一条数
            Products products = productsMapper.selectById(dto.getProductId());
            ShoppingCar shoppingCar = new ShoppingCar();
            shoppingCar.setNumber(1);
            shoppingCar.setProductId(dto.getProductId());
            shoppingCar.setUserId(1L);
            shoppingCar.setName(products.getName());
            shoppingCar.setImage(products.getImageUrl());

            shoppingCarMapper.insert(shoppingCar);
        }
    }

    /**
     * 删除购物车的一个商品
     * @param dto
     */
    public void delete(ShoppingCarDTO dto) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("product_id",dto.getProductId());
        map.put("user_id",BaseContext.getCurrentId());
        //查询购物车是否有数据
        List<ShoppingCar> shoppingCars = shoppingCarMapper.selectByMap(map);
        ShoppingCar shoppingCar = shoppingCars.get(0);
        //判断数据的数量要是为1直接删除字段
        if (shoppingCar.getNumber()==1){
            shoppingCarMapper.deleteById(shoppingCar);
        } else if (shoppingCar.getNumber()>1) {
            //不为1数量减一
            shoppingCar.setNumber(shoppingCar.getNumber()-1);
            shoppingCarMapper.updateById(shoppingCar);
        }

    }

    /**
     * 批量删除
     */
    public void clean(List<Long> ids) {
        Long userId = BaseContext.getCurrentId();

        QueryWrapper<ShoppingCar> qw = new QueryWrapper<>();

        qw.eq("user_id",userId);
        qw.in("id",ids);

        shoppingCarMapper.delete(qw);
    }
}
